<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属乡镇" prop="townShipId">
        <el-select v-model="queryParams.townShipId" clearable filterable placeholder="请选择">
          <el-option v-for="item in townList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="commodityName">
        <el-input v-model="queryParams.commodityName" placeholder="请输入" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商户名称" prop="businessName">
        <el-input v-model="queryParams.businessName" placeholder="请输入" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 页面操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:label:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:label:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:label:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:label:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- table表格 -->
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属乡镇" align="center" prop="townShipName" />
      <el-table-column label="商品名称" align="center" prop="commodityName" />
      <el-table-column label="商品图片" align="center" prop="priveImg" width="150">
        <template slot-scope="scope">
          <el-image v-for="(item, index) in scope.row.priveImg" :key="index" :src="item" style="width:50px;height:50px;"
            :preview-src-list="scope.row.priveImg">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="商户名称" align="center" prop="businessName" />
      <el-table-column label="商户地址" align="center" prop="businessAddress" />
      <el-table-column label="商品活动价" align="center" prop="discountPrice">
        <template slot-scope="scope">
          {{ scope.row.discountPrice || '暂无' }}
        </template>
      </el-table-column>
      <el-table-column label="原价" align="center" prop="originalPrice" />
      <el-table-column label="优惠开始时间" align="center" prop="startTime">
        <template slot-scope="scope">
          {{ scope.row.startTime || '暂无' }}
        </template>
      </el-table-column>
      <el-table-column label="优惠结束时间" align="center" prop="endTime">
        <template slot-scope="scope">
          {{ scope.row.endTime || '暂无' }}
        </template>
      </el-table-column>
      <el-table-column label="是否上架" align="center" prop="status" width="150" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" active-text="上架"
            inactive-text="下架" @change="commodityOpt(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:label:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:label:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改标签对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="所属乡镇" prop="townShipId">
          <el-select v-model="form.townShipId" clearable filterable placeholder="请选择">
            <el-option v-for="item in townList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="commodityName">
          <el-input v-model="form.commodityName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="商品图片" prop="image">
          <imageUpload v-model="form.image" @uploadImage="uploadimg" @remove="removeImg" :limit="9" />
        </el-form-item>
        <el-form-item label="商户名称" prop="businessAddress">
          <el-input v-model="form.businessName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="商户地址" prop="businessAddress">
          <zxMap :value="latLng" :address="form.businessAddress" @input="mapClick" v-if="open"></zxMap>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" disabled />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" disabled />
        </el-form-item>
        <el-form-item label="商品原价" prop="originalPrice">
          <el-input type="number" :min="0" v-model="form.originalPrice" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="商品活动价" prop="discountPrice">
          <el-input type="number" :min="0" v-model="form.discountPrice" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="商品详情" prop="commodityDetails">
          <editor v-if="open" v-model="form.commodityDetails" :min-height="300" :height="300" />
        </el-form-item>
        <el-form-item label="图文详情" prop="imageTextDetail">
          <editor v-if="open" v-model="form.imageTextDetail" :min-height="300" :height="300" />
        </el-form-item>
        <el-form-item label="优惠开始时间" prop="startTime">
          <el-date-picker v-model="form.startTime" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="优惠结束时间" prop="endTime">
          <el-date-picker v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="购买指南" prop="shoppingDirectory">
          <editor v-if="open" v-model="form.shoppingDirectory" :min-height="300" :height="300" />
        </el-form-item>
        <el-form-item label="是否上架" prop="shoppingDirectory">
          <el-radio v-model="form.status" :label="true" :value="true">上架</el-radio>
          <el-radio v-model="form.status" :label="false" :value="false">下架</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  commodity_add,
  commodity_list,
  commodity_update,
  commodity_delete,
} from "@/api/view/commodity";
import { town_list } from "@/api/view/activity_information"
import ImageUpload from "@/components/UploadImage";
import Editor from "@/components/Editor";
import zxMap from "@/components/map/index";
export default {
  name: "Label",
  components: {
    ImageUpload,
    Editor,
    zxMap
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commodityName: null,
        townShipId: null,
        businessName:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        commodityName: [{ required: true, message: "请输入", trigger: "blur" }],
        image: [{ required: true, message: "请选择", trigger: "change" }],
        commodityDetails: [{ required: true, message: "请输入", trigger: "blur" }],
        // discountPrice: [{ required: true, message: "请输入", trigger: "blur" }],
        businessName: [{ required: true, message: "请输入", trigger: "blur" }],
        businessAddress: [{ required: true, message: "请输入", trigger: "blur" }],
        // startTime: [{ required: true, message: "请选择", trigger: "change" }],
        // endTime: [{ required: true, message: "请选择", trigger: "change" }],
        shoppingDirectory: [{ required: true, message: "请输入", trigger: "blur" }],
        townShipId: [{ required: true, message: "请选择", trigger: "change" }],
        latitude: [{ required: true, message: "请输入", trigger: "blur" }],
        longitude: [{ required: true, message: "请输入", trigger: "blur" }],
        originalPrice: [{ required: true, message: "请输入", trigger: "blur" }],
        imageTextDetail: [{ required: true, message: "请输入", trigger: "blur" }],
        status: [{ required: true, message: "请选择", trigger: "change" }],
      },
      //乡镇列表
      townList: [],
      //经纬度
      latLng: "",
    };
  },
  created() {
    this.getList();
    this.getTwon();
  },
  mounted() {

  },
  methods: {
    /** 查询标签列表 */
    getList() {
      this.loading = true;
      commodity_list(this.queryParams).then((response) => {
        response.rows.forEach(item => {
          let priveImg = []; //图片预览数组
          item.imagesUrl = item.imageScale.split(",");
          item.imagesUrl.forEach((val) => {
            priveImg.push(this.baseurl + val);
            item.priveImg = priveImg;
          });
        });
        this.list = response.rows;
        console.log(this.list, 'response');
        this.total = response.total;
        this.loading = false;
      });
    },
    /* 乡镇列表 */
    getTwon() {
      town_list().then(res => {
        this.townList = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessAddress: null,
        businessName: null,
        commodityDetails: null,
        image: null,
        commodityName: null,
        discountPrice: 0,
        endTime: null,
        imageTextDetail: null,
        latitude: null,
        longitude: null,
        originalPrice: 0,
        shoppingDirectory: null,
        startTime: null,
        status: null,
        townShipId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加标签";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.latLng = row.longitude + "," + row.latitude;
      this.form = row;
      this.open = true;
      this.title = "修改标签";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            commodity_update(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            commodity_add(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除标签编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return commodity_delete(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    // /** 导出按钮操作 */
    // handleExport() {
    //   const queryParams = this.queryParams;
    //   this.$confirm("是否确认导出所有标签数据项?", "警告", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(function () {
    //       return exportLabel(queryParams);
    //     })
    //     .then((response) => {
    //       this.download(response.msg);
    //     });
    // },
    //图片上传事件
    uploadimg(e) {
      this.form.image = e;
    },
    //删除图片事件
    removeImg(e) {
      this.form.image = e;
    },
    //地图事件    
    mapClick(e) {
      let latlng = e.lnglat.split(",");
      this.form.longitude = latlng[0];
      this.form.latitude = latlng[1];
      this.form.businessAddress = e.address;
    },
    //tabel表单商品switch change事件
    commodityOpt(val) {
      commodity_update(val).then(res=>{
        this.msgSuccess(res.msg);
        this.getList();
      });
    },
  },
};
</script>
